package com.xin.beauty.controller;


import com.xin.beauty.pojo.entity.Permission;
import com.xin.beauty.pojo.vo.PermissionVO;
import com.xin.beauty.service.IPermissionService;
import com.xin.beauty.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Xin
 * @since 2025-04-29
 */
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping("/selectAssignedPermission/{roleId}")
    public Result selectAssignedPermission(@PathVariable Long roleId) {
        Map<String, Object> map = permissionService.selectAssignedPermission(roleId);
        return Result.ok(map);
    }

    @PostMapping("/assignPermission")
    public Result assignPermission(Long roleId, Long[] permissionIds) {
        permissionService.assignPermission(roleId, permissionIds);
        return Result.ok("分配成功");
    }

    @GetMapping("/selectPermissionTree")
    public Result selectPermissionTree() {
        System.out.println("PermissionController.list");
        List<PermissionVO> list = permissionService.selectPermissionTree();
        return Result.ok(list);
    }

    // /permission/deleteById?id=12
    // /permission/deleteById/12
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        permissionService.removeById(id);
        return Result.ok("删除成功");
    }

    // /permission/deleteById/1,2,3
    @DeleteMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Integer[] ids) {
        permissionService.removeBatchByIds(Arrays.asList(ids));
        return Result.ok("删除成功");
    }

    // @RequestBody : 封装前台传递过来的JSON格式数据
    // @ResponseBody : 将数据转换为JSON格式返回
    @PostMapping("/add")
    public Result add(@RequestBody Permission permission) {
        permissionService.save(permission);
        return Result.ok("添加成功");
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Permission permission = permissionService.getById(id);
        return Result.ok(permission);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Permission permission) {
        permissionService.updateById(permission);
        return Result.ok("修改成功");
    }
}

